// pages/manage/customer/index.js
const app = getApp();
let config = require("../../../utils/config");
let utils = require("../../../utils/util");
import { http } from '../../../utils/request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    userList: [],
    pageIndex: 1,
    isLoad: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /** 搜索框 */
  keyinput(e){
    this.setData({
      keyword: e.detail.value
    })
  },

  /** 搜索查询 */
  searchTap(){
    this.setData({
      pageIndex: 1,
      keyword: this.data.keyword
    })
    this.queryCustomerData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      pageIndex: 1
    })
    this.queryCustomerData();
  },

  /** 用户列表 */
  queryCustomerData(){
    let that = this;
    wx.showLoading();
    http.requestData("GET","PointExchange/GetPageJson",{
      MemberMobile: this.data.keyword,
      PageIndex: this.data.pageIndex,
      PageSize: 10
    }).then(res=>{
      if(res.Tag == 10){
        if(that.data.pageIndex == 1){
          that.data.userList = [];
        }
        res.Data.forEach(item=>{
          item.CreateTime = utils.formatTime(new Date(item.CreateTime))
          that.data.userList.push(item)
        })
        that.setData({
          userList: that.data.userList,
          isLoad: res.Data.length<10?true:false
        })
      }else{
        http.showToast(res.Message)
      }
      wx.hideLoading();
    }).catch(e=>{
      wx.hideLoading();
    })
  },

  /** 兑换记录 */
  recordsTap(e){
    wx.navigateTo({
      url: '/pages/manage/customer/records/index?mid='+e.currentTarget.dataset.mid,
    })
  },

  /** 去兑换 */
  exchangeTap(e){
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/manage/customer/exchange/index?user='+encodeURIComponent(JSON.stringify(item)),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoad){
      return;
    }
    this.setData({
      pageIndex: this.data.pageIndex+1
    })
    this.queryCustomerData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})